home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C18 / Walrus.cpp < prev   
Encoding:
C/C++ Source or Header  |  2000-05-25  |  779 b   |  26 lines

  1. //: C18:Walrus.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Freezing a strstream
  7. #include <iostream>
  8. #include <strstream>
  9. using namespace std;
  10.  
  11. int main() {
  12.   ostrstream s;
  13.   s << "'The time has come', the walrus said,";
  14.   s << ends;
  15.   cout << s.str() << endl; // String is frozen
  16.   // s is frozen; destructor won't delete
  17.   // the streambuf storage on the heap
  18.   s.seekp(-1, ios::cur); // Back up before NULL
  19.   s.rdbuf()->freeze(0); // Unfreeze it
  20.   // Now destructor releases memory, and
  21.   // you can add more characters (but you
  22.   // better not use the previous str() value)
  23.   s << " 'To speak of many things'" << ends;
  24.   cout << s.rdbuf();
  25. } ///:~
  26.